﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.SignalR;
using signalr.dal;
using System.Collections;

namespace signalr
{
    public class ChatOnLineConnection : PersistentConnection
    {
        public static Hashtable userListHash = new Hashtable();
        protected override Task OnConnected(IRequest request, string connectionId)
        {
            string name = "访客" + userListHash.Count;
            Message msg = new Message();
            msg.fldRecerverName = name;
            msg.fldSendUserName = "System";
            msg.fldMsgDetail = "Welcome!";
            msg.fldIsGroup = "0";
            msg.fldMsgSendTime = DateTime.Now;
            msg.Type = "1000";
            userListHash[name] = connectionId;
            return Connection.Send(connectionId, msg.toJson());
        }

        protected override Task OnReceived(IRequest request, string connectionId, string data)
        {
            Message msg = new Message(data);
            
            if (userListHash[msg.fldRecerverName] != null)
                return Connection.Send(userListHash[msg.fldRecerverName].ToString(), msg.toJson());
            else
                return Connection.Broadcast(msg.toJson());
        }
    }
}